package fm.dongman.modellib.source

import org.json.JSONObject

import fm.dongman.animefm.contractlib.BaseDataSource
import fm.dongman.animefm.contractlib.IModel
import fm.dongman.animefm.contractlib.contract.ComicCourierContract
import fm.dongman.modellib.RemoteHelper
import fm.dongman.modellib.StaticArea.StaticUrl
import fm.dongman.modellib.remote.RemoteOperationHelper
import java.util.ArrayList

/**
 * 新番速递数据源
 * Created by shize on 2017/10/29.
 */

class ComicCourierRepository private constructor() : ComicCourierContract.IComicCourierDataSource {
    override fun getComicList(quarter: String, order: String, callback: BaseDataSource.LoadSourceCallback<List<IModel.IComicModel>>) {
        val jsonObj = RemoteHelper.instance.parseGetUrl(StaticUrl.COMIC_COURIER
                + "season=" + quarter + "&order=" + order + "&is_season=0&is_timeline=0&tucao_num=0&recent_num=0")
        if (jsonObj == null) {
            callback.onDataLoadFiled()
            return
        }
        callback.onDataLoaded(downComicCourier(jsonObj))
    }

    override fun getData(callback: BaseDataSource.LoadSourceCallback<List<Map<String, String>>>) {
        val jsonObj = RemoteHelper.instance.parseGetUrl(StaticUrl.COMIC_COURIER
                + "season=0&order=hot&is_season=1&is_timeline=0&tucao_num=0&recent_num=0")
        if (jsonObj == null) {
            callback.onDataLoadFiled()
            return
        }
        callback.onDataLoaded(downSeasons(jsonObj))
    }

    /**
     * 获取季度列表
     */
    private fun downSeasons(jsonObject: JSONObject): List<Map<String, String>> {
        val seasons = ArrayList<Map<String, String>>()
        val jsonObj: JSONObject
        try {
            jsonObj = jsonObject.getJSONObject("data").getJSONObject(ARRAY_SEASONS)
            jsonObj.keys().forEach {
                seasons.add(mapOf(Pair(it, jsonObj.getString(it))))
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return seasons
    }

    /**
     * 新番速递
     */
    private fun downComicCourier(jsonObject: JSONObject): List<IModel.IComicModel> {
        val models = ArrayList<IModel.IComicModel>()
        try {
            val jsonArray = jsonObject.getJSONObject("data").getJSONArray(ARRAY_XINFAN)
            (0 until jsonArray.length()).forEach {
                val jsonObj = jsonArray.getJSONObject(it)
                models.add(RemoteOperationHelper.getOperationHelper().parseComicModel(jsonObj))
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return models
    }

    override fun saveData(dataModel: List<Map<String, String>>) {}

    override fun refreshData() {}

    override fun offLine() {}

    companion object {
        val ORDER_HOT = "hot" // 最热排序
        val ORDER_NEW = "new" // 最新排序
        val ARRAY_SEASONS = "seasons" // 季度列表
        val ARRAY_XINFAN = "xinfan" // 该季度番剧列表

        private var SINGLETON: ComicCourierRepository? = null

        val instance: ComicCourierRepository
            get() {
                if (SINGLETON == null) {
                    SINGLETON = ComicCourierRepository()
                }
                return SINGLETON!!
            }
    }
}
